package com.baibei.ebec.moments.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.baibei.ebec.home.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by tang on 2018/5/15.
 */

public class MomentsHomeAdapter extends RecyclerView.Adapter<MomentsHomeAdapter.ViewHolder> implements View.OnClickListener {

    private ArrayList<JSONObject> arr;


    public MomentsHomeAdapter(ArrayList<JSONObject> arr){
        this.arr = arr;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_moments_home, null);
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        JSONObject job = arr.get(i);
        viewHolder.tvComment.setText(job.optString("commentCount"));
        viewHolder.tvContent.setText(job.optString("contents"));
//        viewHolder.tvShare.setText();
        viewHolder.tvThumb.setText(job.optString("clickCount"));
        viewHolder.tvTime.setText(job.optString("createDate"));
        viewHolder.ivThumb.setImageResource("1".equals(job.optString("state"))?R.drawable.icon_thumb_red:R.drawable.icon_thumb);
        viewHolder.tvUsername.setText(job.optString("nickName"));
//        viewHolder.tvUsername.setText((i+1)+"");


        viewHolder.itemView.setTag(i);
        viewHolder.tvUsername.setTag(i);
        viewHolder.ivThumb.setTag(i);
        viewHolder.ivComment.setTag(i);
        viewHolder.ivShare.setTag(i);
        viewHolder.tvContent.setTag(i);
    }

    @Override
    public int getItemCount() {
        return arr.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        private final ImageView ivAvatar;
        private final TextView tvUsername;
        private final TextView tvContent;
        private final TextView tvTime;
        private final ImageView ivThumb;
        private final TextView tvThumb;
        private final ImageView ivComment;
        private final TextView tvComment;
        private final ImageView ivShare;
        private final TextView tvShare;
        private final GridView gv;

        public ViewHolder(View itemView) {
            super(itemView);
            ivAvatar = itemView.findViewById(R.id.iv_avatar);
            tvUsername = itemView.findViewById(R.id.tv_username);
            tvContent = itemView.findViewById(R.id.tv_content);
            tvTime = itemView.findViewById(R.id.tv_time);
            ivThumb = itemView.findViewById(R.id.iv_thumb);
            tvThumb = itemView.findViewById(R.id.tv_thumb);
            ivComment = itemView.findViewById(R.id.iv_comment);
            tvComment = itemView.findViewById(R.id.tv_comment);
            ivShare = itemView.findViewById(R.id.iv_share);
            tvShare = itemView.findViewById(R.id.tv_share);
            gv = itemView.findViewById(R.id.gv);

            itemView.setOnClickListener(MomentsHomeAdapter.this);
            tvUsername.setOnClickListener(MomentsHomeAdapter.this);
            ivThumb.setOnClickListener(MomentsHomeAdapter.this);
            ivComment.setOnClickListener(MomentsHomeAdapter.this);
            ivShare.setOnClickListener(MomentsHomeAdapter.this);
        }
    }

    @Override
    public void onClick(View view) {
        int position = (int) view.getTag();
        if (onRecyclerViewItemClickListener != null){
            if (view.getId() == R.id.tv_username){
                onRecyclerViewItemClickListener.onClick(view,ViewName.USERNAME,position);
            } else if (view.getId() == R.id.iv_thumb){
                onRecyclerViewItemClickListener.onClick(view,ViewName.THUMB,position);
            } else if (view.getId() == R.id.iv_comment){
                onRecyclerViewItemClickListener.onClick(view,ViewName.COMMENT,position);
            } else if (view.getId() == R.id.iv_share){
                onRecyclerViewItemClickListener.onClick(view,ViewName.SHARE,position);
            } else {
                onRecyclerViewItemClickListener.onClick(view,ViewName.ITEM,position);
            }
        }
    }

    public enum ViewName{
        ITEM,
        USERNAME,
        THUMB,
        COMMENT,
        SHARE
    }

    public interface onRecyclerViewItemClickListener{
        void onClick(View view,ViewName viewName,int position);
    }

    private onRecyclerViewItemClickListener onRecyclerViewItemClickListener = null;

    public void setOnRecyclerViewItemClickListener(onRecyclerViewItemClickListener listener){
        this.onRecyclerViewItemClickListener = listener;
    }
}
